//
//  StorageCenter.swift
//  JLDiaryApp
//
//  Created by Jiang Chencheng on 2024/9/28.
//

import Foundation


class StorageCenter {
    
    static func get<Value: DataConvertible>(key: String) throws -> Value?{
        guard let data = UserDefaults.standard.data(forKey: key) else {
            return nil
        }
        return try Value(data: data)
    }

    
    static func save<Value: Storable>(value: Value?) throws {
        guard let value else {
            UserDefaults.standard.setValue(nil, forKey: Value.key)
            return
        }
        let data = try value.toData()
        UserDefaults.standard.setValue(data, forKey: Value.key)
    }
    
    static func delete(key: String) throws {
        UserDefaults.standard.setValue(nil, forKey: key)
    }
}
